#论坛系统登录页
import time
from selenium.webdriver.common.by import By
from common.com import ForumDriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.wait import WebDriverWait


class ForumLogin:
    driver = ""
    url = ""
    def __init__(self):
        self.url = "http://127.0.0.1:58080/sign-in.html"
        self.driver = ForumDriver.driver
        self.driver.get( self.url )

    def LoginSuTest(self):
        # 清理输入框
        self.driver.find_element( By.CSS_SELECTOR, "#username" ).clear()
        self.driver.find_element( By.CSS_SELECTOR, "#password" ).clear()
        # 输入正确的账号和密码
        self.driver.find_element( By.CSS_SELECTOR, "#username" ).send_keys( "xiaohe1" )
        time.sleep( 1 )  # 模拟输入延迟
        self.driver.find_element( By.CSS_SELECTOR, "#password" ).send_keys( "123" )
        time.sleep( 1 )  # 模拟输入延迟
        self.driver.find_element( By.CSS_SELECTOR, "#submit" ).click()
        ForumDriver.getScreenShot()
        # 登陆成功，查看首页元素是否存在
        # 等待首页元素加载，增加稳定性
        time.sleep(3)
        WebDriverWait( self.driver, 10 ).until(EC.visibility_of_element_located( (By.CSS_SELECTOR, "#article_list_board_title") ))
        # 截图保存
        ForumDriver.getScreenShot()



    def LoginFailTest(self):
        # 1.不输入用户名 不输入密码，直接点击登录
        self.driver.find_element( By.CSS_SELECTOR, "#username" ).clear()
        self.driver.find_element( By.CSS_SELECTOR, "#password" ).clear()
        time.sleep( 1 )  # 模拟输入延迟
        self.driver.find_element( By.CSS_SELECTOR, "#submit" ).click()
        time.sleep( 3 )  # 模拟输入延迟
        # 提示：用户名不能为空
        # 等待错误提示元素加载
        error_element = WebDriverWait( self.driver, 10 ).until(EC.visibility_of_element_located( (By.CSS_SELECTOR, "#signInForm > div.mb-3 > div") ))
        text = error_element.text
        ForumDriver.getScreenShot()  # 截图
        assert text == "用户名不能为空"
        time.sleep( 1 )  # 模拟输入延迟

        # 2.输入正确的用户名、错误的密码
        # 清理输入框
        self.driver.find_element( By.CSS_SELECTOR, "#username" ).clear()
        self.driver.find_element( By.CSS_SELECTOR, "#password" ).clear()
        time.sleep( 1 )  # 模拟输入延迟
        self.driver.find_element( By.CSS_SELECTOR, "#username" ).send_keys( "xiaohe1" )
        time.sleep( 1 )  # 模拟输入延迟
        self.driver.find_element( By.CSS_SELECTOR, "#password" ).send_keys( "123" )
        time.sleep( 1 )  # 模拟输入延迟
        self.driver.find_element( By.CSS_SELECTOR, "#submit" ).click()
        time.sleep( 3 )  # 模拟输入延迟
        ForumDriver.getScreenShot()  # 截图



